package cn.big.bigd.biocode.controller;


import cn.big.bigd.biocode.dao.RateDAO;
import cn.big.bigd.biocode.dto.Result;
import cn.big.bigd.biocode.model.Rate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 *@Author wangfan
 *@Date 2018/1/18 16:11
 */


@RestController
@RequestMapping("rate")
public class RateController {

    @Autowired
    RateDAO rateDAO;










    @RequestMapping(value = "tools/save",method = RequestMethod.POST)
    public Result tool_save(Model model, Rate rate) {


        System.out.println(rate.toString());
        System.out.println(rate.getId());
        if(null==rate.getId()||rate.getId()<1){
            Rate o =rateDAO.findByRaterIdAndRateableTypeAndRateableIdAndDimension(rate.getRaterId(),rate.getRateableType(),rate.getRateableId(),rate.getDimension());
//            rate.setId(null);
            if(null!=o){
                rate.setId(o.getId());
            }
            rate.setCreatedAt(new Date());

        }

        rate.setUpdatedAt(new Date());

        rate =rateDAO.save(rate);
        return Result.ok(rate);
    }





}
